package cn.tedu;

import java.sql.*;

public class Demo03 {
    public static void main(String[] args) throws SQLException {
        //1.获取数据库连接
        Connection conn =
                DriverManager.getConnection("jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai", "root", "root");
        System.out.println("连接对象:"+conn);
        //2.创建执行sql语句的对象,传输器对象
        Statement s = conn.createStatement();
        //3.执行sql
        //>1执行插入数据的sql
        //s.executeUpdate("insert into emp(name) values ('tom')");
        //>2执行修改数据的sql,将tom改为jerry
        //s.executeUpdate("update emp set name = 'jerry' where name = 'tom'");
        //>3执行删除数据的sql,将jerry删除
        //s.executeUpdate("delete from emp where name = 'jerry'");
        //>4执行查询的sql,得到结果集
        ResultSet rs = s.executeQuery("select name,sal from emp");
        while (rs.next()) {
            //获取游标指向这个数据的某一个字段的值,通过字段名获取
            //String name = rs.getString("name");
            //double sal = rs.getDouble("sal");
            //通过查询数据的位置顺序获取数据
            String name = rs.getString(1);
            double sal = rs.getDouble(2);
            System.out.println(name+":"+sal);
        }
        //4.关闭连接
        conn.close();
        System.out.println("执行完成!");
    }
}
